package com.juno.cafenoa.menu;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;

import com.juno.cafenoa.CafeNoa;
import com.juno.cafenoa.R;
import com.juno.cafenoa.cart.CartBasicItem;
import com.juno.cafenoa.menu.MenuTableView.ClickListener;
import com.juno.cafenoa.sqlite.CafeNoaDBAdapter;

public class Menu extends LinearLayout {

	Context context;
	private MenuTableView menuTableView;
	ArrayList<MenuBasicItem> menuItem; // 이게 왜 menuItem임? - - 변수명명좀 제대로
	
	CafeNoaDBAdapter menuDB = null;
	public Menu(Context context, int index) {
		super(context);
		this.context = context;
		init(index);
	}


	/**
	 * resetTableView -> clearTableView
	 */
	public void clearTableView() {
		menuItem.clear();
		menuTableView.clearMenuList();
	}

	private void init(int categoryNumber) {
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.order_detail_view, this);
		menuItem = new ArrayList<MenuBasicItem>();
		menuTableView = (MenuTableView) findViewById(R.id.orderDetailTableView);

		CustomClickListener listener = new CustomClickListener();
		menuTableView.setClickListener(listener);

		// Menu Detail DB와 Cart DB에서 각 세부 물품과 이전에 장바구니에 있는지 받아와야함
		CafeNoa cafeNoa = (CafeNoa) context.getApplicationContext();
		menuDB = new CafeNoaDBAdapter();
		menuDB.open(context);
		ArrayList<MenuBasicItem> dbMenuList = menuDB.selectMenuData(categoryNumber);
		Log.d("Menu.java", "dbMenuList.size() = "+dbMenuList.size());
		for(int i = 0; i < dbMenuList.size(); i++)
		{
			Log.d("CafeNoa", "dbMenuList.get(i).getImgUrl = " + dbMenuList.get(i).getProductName());
			Log.d("CafeNoa", "dbMenuList.get(i).getImgUrl = " + dbMenuList.get(i).getImgUrl());
			menuTableView.addMenuBasicItem(dbMenuList.get(i));
		}
		
		
		// 0321 김용택 Menu Click시 Product Detail에 들어갈 정보(Product Name)리스트
//		for(int i = 0; i < menuTableView.getProductCountInMenu(); i++)
//		{
//			MenuBasicItem menuBasicItem = menuTableView.getProductInMenu(i);
//			menuItem.add(menuBasicItem);
//		}
		OnCheckedChangeListener checkedChangeListener = new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// Cart DB에 Insert, Delete 하는 루틴
				Log.d("Menu.java", "onCheckedChange index = " + buttonView.getTag());
				int index = Integer.valueOf("" + buttonView.getTag());
				MenuBasicItem checkedMenu = menuTableView.getProductInMenu(index);
				if (isChecked == true) {
					CafeNoa addCartFromMenu = (CafeNoa) context
							.getApplicationContext();
					if (!addCartFromMenu.isExistInCart(checkedMenu.getProductName())) {
						addCartFromMenu.addCartList(new CartBasicItem(true,
								checkedMenu.getImgUrl(), checkedMenu
										.getProductName(), checkedMenu.getPrice(), checkedMenu.getProductOption()));
					}
					Toast.makeText(context, "장바구니에 추가되었습니다. option = " + checkedMenu.getProductOption(),
							Toast.LENGTH_SHORT).show();
				} else {
					CafeNoa deleteCartFromMenu = (CafeNoa) context
							.getApplicationContext();
					Toast.makeText(
							context,
							"장바구니에서 삭제 결과 = "
									+ deleteCartFromMenu
											.removeCartList(checkedMenu
													.getProductName()),
							Toast.LENGTH_SHORT).show();
				}
			}
		};

		menuTableView.setCheckedChangeListener(checkedChangeListener);
		menuTableView.commit();

	}
	
	public void refreshCheckBox()
	{
		MenuBasicItem menuBasicItem;
		Log.d("refreshCheckBox()", "refreshCheckBox() 호출됨 menuTableView.GetCount() = " + menuTableView.getProductCountInMenu());
		for(int i = 0; i < menuTableView.getProductCountInMenu(); i++)
		{
			
			menuBasicItem = menuTableView.getProductInMenu(i);
			String productName = menuBasicItem.getProductName();

			CafeNoa cafeNoa = (CafeNoa) context.getApplicationContext();
			
			// 장바구니에 없는 항목이면 false
			if(!cafeNoa.isExistInCart(productName))
			{
				// 해당 view(한줄)을 찾음
				View refreshTargetItem = null;
				refreshTargetItem = menuTableView.getViewListContainer().getChildAt(i);
				
				// view안에 CheckBox를 찾음
				CheckBox cbChoosedProduct = (CheckBox)refreshTargetItem.findViewById(R.id.cbChoosedProduct);
				cbChoosedProduct.setChecked(cafeNoa.isExistInCart(productName));
				
			}
		}

//		menuTableView.getmListContainer().invalidate();
		/**
		 * Refresh 루틴 추가
		 */
	}

	protected MenuTableView getUITableView() {
		return menuTableView;
	}

	// 0321 김용택 : Menu Click시 Product Detail구현
	private class CustomClickListener implements ClickListener {
		IntroduceMenuAlertView alertView;
		@Override
		public void onClick(final int index) {
			Log.d("Menu.java", "CustomClickListener is called");
			alertView = new IntroduceMenuAlertView(
					context, menuTableView.getProductInMenu(index));
			AlertDialog.Builder ab = new AlertDialog.Builder(context);
			ab.setTitle(menuTableView.getProductInMenu(index).getProductName());
			ab.setView(alertView);
			ab.setPositiveButton("확인", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					
				}
			});

			
			AlertDialog alert = ab.create();
			alert.show();
		}
	}
}
